//
//  WBMainTabBarController.swift
//  Weibo
//
//  Created by 李磊 on 2017/6/5.
//  Copyright © 2017年 李磊www. All rights reserved.
//

import UIKit



class WBMainTabBarController: UITabBarController {
    
    
    lazy var composeBtn: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        setupComposeBtn()
    }
    
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }

    // MARK: - 监听方法
    /// 撰写微博
    // FIXME: 没有实现
    func composeStatus() {
        print("撰写微博")
    }
    
    
    
}


//extension 类似于OC中的分类  在 Swift还可以用来切分代码

extension WBMainTabBarController {
    
    
    
     /// 设置子控制器
      func setupChildControllers (){
        
//        let array = [
//            ["clsName": "WBHomeViewController","title": "首页","imageName": "home"],
//            ["clsName": "WBMessageViewController","title": "消息","imageName": "message_center"],
//            ["clsName": "UIViewController"],
//            ["clsName": "WBDiscoverViewController","title": "发现","imageName": "discover"],
//            ["clsName": "WBProfileViewController","title": "我","imageName": "profile"]
//        ]
        
        guard let path = Bundle.main.path(forResource: "tabbarConfig", ofType: "json") ,
                let array = NSArray(contentsOfFile: path)
        else {
            return
        }
        
        
        var vcArray = [UIViewController]()
        for dict in array {
            
            vcArray.append(controller(dict: dict as! [String : String]))
            
        }
        
        viewControllers = vcArray
    
    }
    
    // 设置中间按钮
    func setupComposeBtn() {
        
        tabBar.addSubview(composeBtn)
        let count = CGFloat(childViewControllers.count)
        let w = view.bounds.width / count
        
        composeBtn.frame.origin.x = 2 * w
        
        // 按钮监听方法
        composeBtn.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
    }
    
    
    /// 通过字典创建控制器
    func controller(dict:[String: String]) -> UIViewController {
        //1、 获取字典内容
        guard let clsName = dict["clsName"],
              let title = dict["title"],
              let imageName = dict["imageName"],
              let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName) as? UIViewController.Type else {
                // 容错处理
                return UIViewController()
        }
        
        // 2、 创建视图控制器
        let vc = cls.init()
        vc.title = title
        // 3、 设置图片
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        // 4、 设置tabbar标题字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        
        let nav = WBNaviViewController(rootViewController: vc)
        
        return nav
        
    }
    

    

}
